package weather;

import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

public class Station implements IRequest {
	
	private String location;
	private ArrayList<IResponse> requesters;
	private Weather current;
	
	public Station(String location, IResponse requester) {
		this.location = location;
		this.requesters = new ArrayList<IResponse>();
		if (requester != null) {
			registerRequester(requester);
		}
		this.weather = new Weather(this.location);
		this.initialize();
	}
	
	public static void initialize() {
		int span = (new Random()).nextInt(4) + 2;
		(new Timer()).scheduleAtFixedRate(new TimerTask() {
			
			@Override
			public void run() {
				current = new Weather(this.location);
			}
		}, 0, 1000 * span);
	}
	
	public void measure() {
		for (IResponse requester: this.requesters) {
			requester.answer(current);
		}
	}
	
	public void registerRequester(IResponse requester) {
		requesters.add(requester);
	}
}